﻿using Abs.Core.Models.User;
using Abs.Core.Util;
using NUnit.Framework;

namespace Abs.Core.UnitTesting
{
    //[TestFixture]
    public class UserRepository
    {
        private DAL.AdoNet.UserRepository Repository { get; set; }
        private User User { get; set; }

        //[TestFixtureSetUp]
        public void BeforeTests()
        {
            this.User = new User
                        {
                            Email = "toph@abs.com",
                            Name = "Toph",
                            Password = "123456",
                            RoleId = (int) RoleEnum.User
                        };
        }//end of public void BeforeTests()

        //[SetUp]
        public void BeforeEachTest()
        {
            this.Repository = new DAL.AdoNet.UserRepository();
        }//end of internal void BeforeEachTest()

        //[TearDown]
        public void AfterEachTest()
        {
            if(this.Repository != null)
                this.Repository.Dispose();
        }//end of internal void AfterEachTest()

        //[Test]
        public void CreateUser()
        {
            var isSuccess = this.Repository.Create(this.User);
            Assert.True(isSuccess);
        }//end of public void CreateUser()

        //[Test]
        public void DuplicateCreateUser()
        {
            var isSuccess = this.Repository.Create(this.User);
            Assert.False(isSuccess);
        }//end of public void CreateDuplicateUser()

        //[Test]
        public void EditExistingUser()
        {
            this.User.Name = "Toph Earth";
            var isSuccess = this.Repository.Update(this.User);
            Assert.True(isSuccess);
        }//end of public void EditExistingUser()

        //[Test]
        public void EditNoExistingUser()
        {
            var user = new User
                       {
                           Email = "cat@abs.com",
                           Name = "Cat",
                           Password = Sha512.Hashing("123456"),
                           RoleId = 1
                       };
            var isSuccess = this.Repository.Update(user);
            Assert.False(isSuccess);
        }//end of public void EditNoExistingUser()

        //[Test]
        public void ReadAllUsers()
        {
            var totalUsers = this.Repository.SelectAll().Count;
            var expectedTotalUsers = 11;
            Assert.AreEqual(expectedTotalUsers, totalUsers);
        }//end of public void ReadAllUsers()

        //[Test]
        public void ReadExistingUser()
        {
            var id = new object[] { this.User.Email };
            var user = this.Repository.Select(id);
            Assert.IsNotNull(user);
        }//end of public void ReadExistingUser()

        //[Test]
        public void ReadNoExistingUser()
        {
            var id = new object[] { "cat@abs.com" };
            var user = this.Repository.Select(id);
            Assert.IsNull(user);
        }//end of public void ReadNoExistingUser()

        //[Test]
        public void RemoveExistingUser()
        {
            var id = new object[] {this.User.Email};
            var isSuccess = this.Repository.Delete(id);
            Assert.IsTrue(isSuccess);
        }//end of public void RemoveExistingUser()

        //[Test]
        public void RemoveNoExistinguser()
        {
            var id = new object[] { "cat@abs.com" };
            var isSuccess = this.Repository.Delete(id);
            Assert.IsFalse(isSuccess);
        }//end of public void RemoveNoExistinguser()

        //[Test]
        public void LogInExistingUser()
        {
            var logIn = new LogIn
                        {
                            Email = this.User.Email,
                            Password = "123456"
                        };
            var user = this.Repository.LogIn(logIn);
            Assert.IsNotNull(user);
        }//end of public void LogIn()

        //[Test]
        public void LogInNoExistingUser()
        {
            var logIn = new LogIn
            {
                Email = this.User.Email,
                Password = "1234567"
            };
            var user = this.Repository.LogIn(logIn);
            Assert.IsNull(user);
        }//end of public void LogIn()
    }//end of internal class UserRepository
}//end of namespace